package com.transino.bop.domain;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.kanas.framework.support.AbstractModel;

@XmlRootElement(name = "property")
public class Property extends AbstractModel implements Cloneable,Comparable<Property> {

	private static final long serialVersionUID = 1L;
	
	private String name;
	private String label;
	private String type;
	private String length;
	private String points;
	private Boolean required;
	private Boolean defaultShow;
	private Boolean optionFlag;
	private Integer index;
	private Integer colspan;
	private String width;
	private String regExp;
	private String constraints;
	private String showType;
	private String dicType;
	private String groupId;
	private String validator;
	private String invalidMessage;
	private String refEntity;
	private Entity entity;
	private Entity referenceEntity;
	private PropertyGroup group;
	private List<Option> options;
	private List<Reference> references;
	
	@XmlAttribute(name = "name")
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@XmlAttribute(name = "type")
	public String getType() {
		if(type == null) type = "";
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	@XmlAttribute(name = "length")
	public String getLength() {
		return length;
	}
	
	public void setLength(String length) {
		this.length = length;
	}
	
	@XmlAttribute(name = "points")
	public String getPoints() {
		return points;
	}
	
	public void setPoints(String points) {
		this.points = points;
	}
	
	@XmlAttribute(name = "required")
	public Boolean getRequired() {
		if(required == null) required = false;
		return required;
	}

	public void setRequired(Boolean required) {
		this.required = required;
	}
	
	@XmlAttribute(name = "index")
	public Integer getIndex() {
		if(index == null) index = 100;
		return index;
	}
	
	public void setIndex(Integer index) {
		this.index = index;
	}

	@XmlAttribute(name = "regExp")
	public String getRegExp() {
		if(regExp == null) regExp = "";
		return regExp;
	}

	public void setRegExp(String regExp) {
		this.regExp = regExp;
	}

	@XmlAttribute(name = "constraints")
	public String getConstraints() {
		if(constraints == null) constraints = "''";
		return constraints;
	}

	public void setConstraints(String constraints) {
		this.constraints = constraints;
	}
	
	public String getDataType() {
		if(type == null) return "";
		else if("char".equalsIgnoreCase(type)||"varchar".equalsIgnoreCase(type)||"string".equalsIgnoreCase(type))return "String";
		else if("decimal".equalsIgnoreCase(type)||"number".equalsIgnoreCase(type)) return "Number";
		return "";
	}

	public void setDataType(String dataType) {
	}

	@XmlAttribute(name = "showType")
	public String getShowType() {
		if(showType==null || showType=="") {
			if(type!=null && !type.equals("") && type.equals("decimal")) {
				showType = "dijit/form/NumberTextBox";
			}else {
				showType = "dijit/form/ValidationTextBox";
			}
		}
		return showType;
	}
	
	public void setShowType(String showType) {
		this.showType = showType;
	}

	@XmlAttribute(name = "dicType")
	public String getDicType() {
		if(dicType == null) dicType = "";
		return dicType;
	}

	public void setDicType(String dicType) {
		this.dicType = dicType;
	}

	public Entity getEntity() {
		return entity;
	}

	public void setEntity(Entity entity) {
		this.entity = entity;
	}

	@XmlAttribute(name = "label")
	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	@XmlAttribute(name = "defaultShow")
	public Boolean getDefaultShow() {
		if(defaultShow==null)defaultShow = false;
		return defaultShow;
	}

	public void setDefaultShow(Boolean defaultShow) {
		this.defaultShow = defaultShow;
	}

	@XmlAttribute(name = "colspan")
	public Integer getColspan() {
		if(colspan==null) colspan = 1;
		return colspan;
	}

	public void setColspan(Integer colspan) {
		this.colspan = colspan;
	}

	@XmlAttribute(name = "width")
	public String getWidth() {
		if(width == null) width = "100%";
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	@XmlAttribute(name = "groupId")
	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	@Override
	public String toIdString() {
		return this.name;
	}

	@Override
	public int compareTo(Property o) {
		if(this.index==null || o.getIndex()==null) {
			return 1;
		}else {
			return this.index - o.getIndex();
		}
	}

	public PropertyGroup getGroup() {
		return group;
	}

	public void setGroup(PropertyGroup group) {
		this.group = group;
	}

	@XmlElement(name = "validator")
	public String getValidator() {
		if(validator == null) validator = "";
		return validator;
	}

	public void setValidator(String validator) {
		this.validator = validator;
	}

	@XmlAttribute(name = "invalidMessage")
	public String getInvalidMessage() {
		if(invalidMessage == null) invalidMessage = "";
		return invalidMessage;
	}

	public void setInvalidMessage(String invalidMessage) {
		this.invalidMessage = invalidMessage;
	}

	@XmlAttribute(name = "refEntity")
	public String getRefEntity() {
		if(refEntity == null) refEntity = "";
		return refEntity;
	}

	public void setRefEntity(String refEntity) {
		this.refEntity = refEntity;
	}

	@XmlElement(name = "option")
	public List<Option> getOptions() {
		if(options == null) options = new ArrayList<Option>();
		return options;
	}

	public void setOptions(List<Option> options) {
		this.options = options;
	}

	@XmlAttribute(name = "optionFlag")
	public Boolean getOptionFlag() {
		if(optionFlag == null) optionFlag = false;
		return optionFlag;
	}

	public void setOptionFlag(Boolean optionFlag) {
		this.optionFlag = optionFlag;
	}

	@XmlElement(name = "reference")
	public List<Reference> getReferences() {
		if(references == null) references = new ArrayList<Reference>();
		return references;
	}

	public void setReferences(List<Reference> references) {
		this.references = references;
	}

	public Entity getReferenceEntity() {
		return referenceEntity;
	}

	public void setReferenceEntity(Entity referenceEntity) {
		this.referenceEntity = referenceEntity;
	}
	
}
